/*Copyright © 2008 Cassarin-Grand Arthur (Zoloom)

This file is part of PlayOnLinux Script Creator.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */

#include "Fenp.h"
#include "engine/files.h"

using namespace std;

Fenp::Fenp()
{
	setIconSize(QSize(22,22)); //allow wndow to show normally tool bar icons
	intNumberTabs = 1;
	
	//################ MENUS ################
	
	//------------- FILE MENU
	
	QMenu *menuFile = menuBar()->addMenu(tr("&File"));
	
	QAction *actionNewDocument = new QAction(tr("&New"), this);
		//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
		actionNewDocument->setShortcut(QKeySequence("Ctrl+N"));
		actionNewDocument->setIcon(QIcon("../bin/images/16x16/new.png"));
    menuFile->addAction(actionNewDocument);
	
	QAction *actionOpenDocument = new QAction(tr("&Open"), this);
		connect(actionOpenDocument, SIGNAL(triggered()), this, SLOT(openFile ()));
		actionOpenDocument->setShortcut(QKeySequence("Ctrl+O"));
		actionOpenDocument->setIcon(QIcon("../bin/images/16x16/open.png"));
    menuFile->addAction(actionOpenDocument);
	
	QAction *actionSaveDocument = new QAction(tr("&Save"), this);
		//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
		actionSaveDocument->setShortcut(QKeySequence("Ctrl+S"));
		actionSaveDocument->setIcon(QIcon("../bin/images/16x16/save.png"));
    menuFile->addAction(actionSaveDocument);
	
	QAction *actionSaveAsDocument = new QAction(tr("&Save As"), this);
		//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
		actionSaveAsDocument->setShortcut(QKeySequence("Ctrl+A"));
		actionSaveAsDocument->setIcon(QIcon("../bin/images/16x16/save-as.png"));
    menuFile->addAction(actionSaveAsDocument);
	
	menuFile->addSeparator();
			
	QAction *actionQuit = new QAction(tr("&Quit"), this);
		//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
		actionQuit->setShortcut(QKeySequence("Ctrl+Q"));
		actionQuit->setIcon(QIcon("../bin/images/16x16/quit.png"));
    menuFile->addAction(actionQuit);
			
	//------------- EDIT MENU
	
    QMenu *menuEdit = menuBar()->addMenu(tr("&Edit"));
	
	QAction *actionCopy = new QAction(tr("&Copy"), this);
		connect(actionCopy, SIGNAL(triggered()), this, SLOT(copyText()));
		actionCopy->setShortcut(QKeySequence("Ctrl+C"));
		actionCopy->setIcon(QIcon("../bin/images/16x16/copy.png"));
    menuEdit->addAction(actionCopy);
	
	QAction *actionCut = new QAction(tr("&Cut"), this);
		connect(actionCut, SIGNAL(triggered()), this, SLOT(cutText()));
		actionCut->setShortcut(QKeySequence("Ctrl+X"));
		actionCut->setIcon(QIcon("../bin/images/16x16/cut.png"));
    menuEdit->addAction(actionCut);
	
	QAction *actionPaste = new QAction(tr("&Paste"), this);
		connect(actionPaste, SIGNAL(triggered()), this, SLOT(pasteText()));
		actionPaste->setShortcut(QKeySequence("Ctrl+V"));
		actionPaste->setIcon(QIcon("../bin/images/16x16/paste.png"));
    menuEdit->addAction(actionPaste);
	
	QAction *actionUndo= new QAction(tr("&Undo"), this);
		connect(actionUndo, SIGNAL(triggered()), this, SLOT(undo()));
		actionUndo->setShortcut(QKeySequence("Ctrl+Z"));
		actionUndo->setIcon(QIcon("../bin/images/16x16/undo.png"));
    menuEdit->addAction(actionUndo);
	
	QAction *actionRedo= new QAction(tr("&Redo"), this);
		connect(actionRedo, SIGNAL(triggered()), this, SLOT(redo()));
		actionRedo->setShortcut(QKeySequence("Ctrl+Y"));
		actionRedo->setIcon(QIcon("../bin/images/16x16/redo.png"));
    menuEdit->addAction(actionRedo);
	
	//------------- INTERFACE MENU
	
	QMenu *menuInterface = menuBar()->addMenu(tr("&Interface"));
	
	QAction *actionInterfaceGUI = new QAction(tr("&GUI settings"), this);
		//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
		//actionRedo->setShortcut(QKeySequence("Ctrl+Y"));
		actionInterfaceGUI->setIcon(QIcon("../bin/images/16x16/gui.png"));
    menuInterface->addAction(actionInterfaceGUI);
	
	QAction *actionInterfaceEditor = new QAction(tr("&Editor settings"), this);
		//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
		//actionRedo->setShortcut(QKeySequence("Ctrl+Y"));
		actionInterfaceEditor->setIcon(QIcon("../bin/images/16x16/editor.png"));
    menuInterface->addAction(actionInterfaceEditor);
	
	//------------- RUN MENU
	
    QMenu *menuRun = menuBar()->addMenu(tr("&Run"));
	
	QAction *actionRun= new QAction(tr("&Run"), this);
		//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
		actionRun->setShortcut(QKeySequence("Ctrl+R"));
		actionRun->setIcon(QIcon("../bin/images/16x16/runG.png"));
    menuRun->addAction(actionRun);
	
	//------------- FUNCTIONS MENU
	
	QMenu *menuFunctions = menuBar()->addMenu(tr("F&unctions"));
	
	QMenu *funcmGeneral = menuFunctions->addMenu(tr("General"));
		funcmGeneral->setIcon(QIcon("../bin/images/16x16/general.png"));
	
	QMenu *funcmDialogs = menuFunctions->addMenu(tr("Dialogs"));
		funcmDialogs->setIcon(QIcon("../bin/images/16x16/dialogs.png"));
	
	QMenu *funcmFilesDirectories= menuFunctions->addMenu(tr("Files and directories"));
		funcmFilesDirectories->setIcon(QIcon("../bin/images/16x16/folder.png"));
	
	//------------ WINE CONFIG MENU AND SUB-MENUS
	
	QMenu *funcmWConfig = menuFunctions->addMenu(tr("Wine configurations"));
		funcmWConfig->setIcon(QIcon("../bin/images/16x16/wconfig.png"));
	
		QMenu *funcmWConfigSound = funcmWConfig->addMenu(tr("Sound"));
			funcmWConfigSound->setIcon(QIcon("../bin/images/16x16/audio.png"));
	
		QMenu *funcmWConfigImage = funcmWConfig->addMenu(tr("Image"));
			funcmWConfigImage->setIcon(QIcon("../bin/images/16x16/image.png"));
	
		QMenu *funcmWConfigHardware = funcmWConfig->addMenu(tr("Hardware"));
			funcmWConfigHardware->setIcon(QIcon("../bin/images/16x16/hardware.png"));
	
	//----------- WINE PREFIXE MENU

	QMenu *funcmWPrefixes = menuFunctions->addMenu(tr("Wine prefixes"));
		funcmWPrefixes->setIcon(QIcon("../bin/images/16x16/wprefixes.png"));
	
	QMenu *funcmDVDCD = menuFunctions->addMenu(tr("DVD/CD-ROM"));
		funcmDVDCD->setIcon(QIcon("../bin/images/16x16/cd.png"));
	
	//------------- HELP MENU
	
	QMenu *menuHelp = menuBar()->addMenu("&?");
	
	QAction *actionWebOfficial = new QAction(tr("&Official Website"), this);
		connect(actionWebOfficial, SIGNAL(triggered()), this, SLOT(openBrowserWebOfficial()));
		actionWebOfficial->setIcon(QIcon("../bin/images/16x16/web.png"));
    menuHelp->addAction(actionWebOfficial);
	
	menuHelp->addSeparator();
	
	QAction *actionWebPOL = new QAction(tr("&PlayOnLinux's Website"), this);
		connect(actionWebPOL, SIGNAL(triggered()), this, SLOT(openBrowserWebPOL()));
		actionWebPOL->setIcon(QIcon("../bin/images/16x16/web.png"));
    menuHelp->addAction(actionWebPOL);
	
	QAction *actionWebDoc = new QAction(tr("&Developer's Documentation"), this);
		connect(actionWebDoc, SIGNAL(triggered()), this, SLOT(openBrowserWebDoc()));
		actionWebDoc->setIcon(QIcon("../bin/images/16x16/web.png"));
    menuHelp->addAction(actionWebDoc);
	
	QAction *actionWebForum = new QAction(tr("&PlayOnLinux's Forums"), this);
		connect(actionWebForum, SIGNAL(triggered()), this, SLOT(openBrowserWebForum()));
		actionWebForum->setIcon(QIcon("../bin/images/16x16/web.png"));
    menuHelp->addAction(actionWebForum);
	
	menuHelp->addSeparator();
	
	QAction *actionAbout= new QAction(tr("&About"), this);
		//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
		actionAbout->setIcon(QIcon("../bin/images/16x16/about.png"));
    menuHelp->addAction(actionAbout);
	
	//################# MDI WINDOW #################
	
	mdiMain = new QMdiArea;
    	setCentralWidget(mdiMain);
		mdiMain->setViewMode(QMdiArea::TabbedView);
	
	//################# TOOL BAR ###################
	
	QToolBar *toolBarMain = addToolBar("Main");
		
		QAction *actionNew32 = new QAction(tr("&New"), this);
			//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
			actionNew32->setIcon(QIcon("../bin/images/22x22/new.png"));
		toolBarMain->addAction(actionNew32);
	
		QAction *actionOpen32 = new QAction(tr("&Open"), this);
			//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
			actionOpen32->setIcon(QIcon("../bin/images/22x22/open.png"));
		toolBarMain->addAction(actionOpen32);
	
		QAction *actionSave32 = new QAction(tr("&Save"), this);
			//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
			actionSave32->setIcon(QIcon("../bin/images/22x22/save.png"));
		toolBarMain->addAction(actionSave32);
	
		QAction *actionSaveAs32 = new QAction(tr("&Save as"), this);
			//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
			actionSaveAs32->setIcon(QIcon("../bin/images/22x22/save-as.png"));
		toolBarMain->addAction(actionSaveAs32);
	
		toolBarMain->addSeparator(); 
	
		QAction *actionUndo32 = new QAction(tr("&Undo"), this);
			//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
			actionUndo32->setIcon(QIcon("../bin/images/22x22/undo.png"));
		toolBarMain->addAction(actionUndo32);
	
		QAction *actionRedo32 = new QAction(tr("&Redo"), this);
			//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
			actionRedo32->setIcon(QIcon("../bin/images/22x22/redo.png"));
		toolBarMain->addAction(actionRedo32);
	
		toolBarMain->addSeparator(); 
	
		QAction *actionRun32 = new QAction(tr("&Run"), this);
			//connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
			actionRun32->setIcon(QIcon("../bin/images/22x22/run.png"));
		toolBarMain->addAction(actionRun32);
	
	//################# STATUS BAR ###################
	
	sbarMain = statusBar();
		sbarMain->showMessage("Ready",5000);	
}
 
void Fenp::showFile(QString fileName, QString buffer)
{			
	QsciScintilla *txteShow = new QsciScintilla;
		txteShow->append(buffer);
		txteShow->setMarginLineNumbers(1,true);
		txteShow->setMarginWidth(1, "-----");
	
	QMdiSubWindow *subWindow = new QMdiSubWindow;
		mdiMain->addSubWindow(txteShow);
		subWindow->setWindowTitle(fileName);
		subWindow->setVisible(true);
}

void Fenp::cutText()
{
	returnCurrentTab()->QsciScintilla::cut();
} 

void Fenp::copyText()
{
	returnCurrentTab()->QsciScintilla::copy();
}

void Fenp::pasteText()
{
	returnCurrentTab()->QsciScintilla::paste();
}

void Fenp::undo()
{
	returnCurrentTab()->QsciScintilla::undo();
}

void Fenp::redo()
{
	returnCurrentTab()->QsciScintilla::redo();
}

//+-+-+-+-+-+-+-+ Durty SOURCE, CORRECT +-+-+-+-+-+-+-+-

void Fenp::openBrowserWebOfficial()
{
	if (QDesktopServices::openUrl(QUrl("http://www.playonlinux.com/fr")) != true)
	{
		QMessageBox::critical(this, "POLSC", tr("Unable to open the requested URL with your default browser."));
	}
}

void Fenp::openBrowserWebPOL()
{
	if (QDesktopServices::openUrl(QUrl("http://www.playonlinux.com/")) != true)
	{
		QMessageBox::critical(this, "POLSC", tr("Unable to open the requested URL with your default browser."));
	}
}

void Fenp::openBrowserWebDoc()
{
	if (QDesktopServices::openUrl(QUrl("http://www.playonlinux.com/en/documentation.html")) != true)
	{
		QMessageBox::critical(this, "POLSC", tr("Unable to open the requested URL with your default browser."));
	}
}

void Fenp::openBrowserWebForum()
{
	if (QDesktopServices::openUrl(QUrl("http://www.playonlinux.com/en/forums.html")) != true)
	{
		QMessageBox::critical(this, "POLSC", tr("Unable to open the requested URL with your default browser."));
	}
}
//-+-+-+-+-+-++-++-+-++-+-+-+-+-+-+-+-+-+-

QsciScintilla *Fenp::returnCurrentTab()
{
    return mdiMain->currentSubWindow()->findChild<QsciScintilla *>();;
}

void Fenp::closeEvent(QCloseEvent *event)
{
	exit(0);
}


